package cn.iocoder.yudao.module.product.evaluation.controller.admin;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.business.basic.pojo.vo.UserSimpleVO;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.api.member.user.MemberUserApi;
import cn.iocoder.yudao.module.api.shop.info.ShopInfoApi;
import cn.iocoder.yudao.module.api.shop.info.dto.ShopInfoDTO;
import cn.iocoder.yudao.module.product.evaluation.dal.dataobject.ProductEvaluationDO;
import cn.iocoder.yudao.module.product.evaluation.service.ProductEvaluationService;
import cn.iocoder.yudao.module.product.evaluation.vo.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;

@Tag(name = "管理后台 - 产品评价")
@RestController
@RequestMapping("/product/comment")
@Validated
public class ProductEvaluationController {

    @Resource
    private ProductEvaluationService productEvaluationService;

    @Resource
    private ShopInfoApi shopInfoApi;
    @Resource
    private MemberUserApi memberUserApi;

    @GetMapping("/page")
    @Operation(summary = "获得产品评价分页")
    @PreAuthorize("@ss.hasPermission('product:comment:query')")
    public CommonResult<PageResult<ProductEvaluationRespVO>> getCommentPage(@Valid ProductEvaluationPageReqVO pageVO) {
        PageResult<ProductEvaluationDO> pageResult = productEvaluationService.getCommentPage(pageVO);
        if (CollUtil.isEmpty(pageResult.getList())) {
            return success(PageResult.empty());
        }
        Set<Long> shopIds = convertSet(pageResult.getList(), ProductEvaluationDO::getShopId);
        Map<Long, ShopInfoDTO> shops = shopInfoApi.getShopInfoMap(shopIds);
        Set<Long> userIds = pageResult.getList().stream().map(ProductEvaluationDO::getUserId).collect(Collectors.toSet());
        Map<Long, UserSimpleVO> users = memberUserApi.getUserSimpleMap(userIds);
        // 拼接返回
        PageResult<ProductEvaluationRespVO> data = BeanUtils.toBean(pageResult, ProductEvaluationRespVO.class);
        data.getList().forEach(vo -> {
            ShopInfoDTO shop = shops.get(vo.getShopId());
            vo.setShop(shop == null ? ShopInfoDTO.FALLBACK : shop);
            if (Boolean.TRUE.equals(vo.getAnonymous())) {
                vo.setUser(UserSimpleVO.ANONYMOUS);
            } else {
                vo.setUser(users.get(vo.getUserId()));
            }
        });
        return success(data);
    }

    @PutMapping("/update-visible")
    @Operation(summary = "显示 / 隐藏评论")
    @PreAuthorize("@ss.hasPermission('product:comment:update')")
    public CommonResult<Boolean> updateCommentVisible(@Valid @RequestBody ProductEvaluationUpdateVisibleReqVO updateReqVO) {
        productEvaluationService.updateCommentVisible(updateReqVO);
        return success(true);
    }

    @PutMapping("/reply")
    @Operation(summary = "回复评论")
    @PreAuthorize("@ss.hasRole('shop_admin')")
    public CommonResult<Boolean> commentReply(@Valid @RequestBody ProductEvaluationReplyReqVO replyVO) {
        productEvaluationService.replyComment(replyVO);
        return success(true);
    }

    @PostMapping("/create")
    @Operation(summary = "添加虚拟评论", description = "自动添加机器人账号并由该机器人账号进行评论")
    @PreAuthorize("@ss.hasRole('shop_admin')")
    public CommonResult<Boolean> createComment(@Valid @RequestBody ProductEvaluationCreateReqVO createReqVO) {
        productEvaluationService.createComment(createReqVO, true);
        return success(true);
    }

}
